package news.paper.tintuc.tinnong.docbao.doctin.adapter;

import java.util.ArrayList;

import news.paper.tintuc.tinnong.docbao.doctin.controller.Controller;
import news.paper.tintuc.tinnong.docbao.doctin.controller.InterfaceAdapter;
import news.paper.tintuc.tinnong.docbao.doctin.util.Constrant;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class MyBaseAdapter<T extends BaseEntity> extends BaseAdapter {

	private Activity activity;
	private ArrayList<T> arrayList;
	private static LayoutInflater inflater = null;
	private int page;
	private int pate_total;
	private boolean isloading;
	private InterfaceAdapter interfaceAdapter;

	public MyBaseAdapter(Activity a, InterfaceAdapter interfaceAdapter_) {
		activity = a;
		page = -1;
		pate_total = 0;
		isloading = false;
		interfaceAdapter = interfaceAdapter_;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// /tesst bitbucket
	}

	public int getCount() {
		return arrayList.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	protected abstract View getView(final int position);

	public View getView(final int position, View convertView, ViewGroup parent) {
		if (position == getCount() - 1
				&& (page + 1) * Controller.perpage < pate_total - 1
				&& !isloading) {
			load_data(Constrant.SUBID);
		}
		return convertView;
	}

	public void load_data(String subid) {
		page++;
		Controller controller = new Controller();
//		controller.listtopnewchap(subid, page + "", new RequestListener() {
//
//			@Override
//			public void success(Object response) {
//				isloading = false;
//				interfaceAdapter.finish_load();
//			}
//
//			@Override
//			public void fail(String message) {
//				isloading = false;
//				interfaceAdapter.finish_load();
//			}
//
//			@Override
//			public void begin() {
//				isloading = true;
//				interfaceAdapter.start_load();
//			}
//		});
	}
}